package com.ihunanren.biz.taobao.open.impl;

import org.springframework.beans.factory.annotation.Autowired;

import com.ihunanren.biz.taobao.domain.TbShop;
import com.ihunanren.biz.taobao.open.ITaobaoShopService;
import com.ihunanren.biz.taobao.open.TaobaoResponseHelper;
import com.ihunanren.biz.taobao.open.TaobaoServiceException;
import com.taobao.api.ApiException;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.Shop;
import com.taobao.api.request.ShopGetRequest;
import com.taobao.api.response.ShopGetResponse;

/**
 * @author kalman03
 */
public class TaobaoShopService implements ITaobaoShopService{

	@Autowired
	private TaobaoClient taobaoClient;

	@Override
	public TbShop getShop(String nick) throws TaobaoServiceException {
		ShopGetRequest req=new ShopGetRequest();
		req.setFields("sid,cid,title,nick,desc,bulletin,pic_path,created,modified");
		req.setNick(nick);
		try {
			ShopGetResponse response = taobaoClient.execute(req);
			if(response != null && response.isSuccess()){
				Shop shop = response.getShop();
				return convertToTbShop(shop);
			}else{
				String errorString = TaobaoResponseHelper.getResponseErrorMsg(response);
				throw new TaobaoServiceException(errorString);
			}
		} catch (ApiException e) {
			throw new TaobaoServiceException(e);
		}
	}
	
	private TbShop convertToTbShop(Shop shop){
		TbShop shop2 = new TbShop();
		shop2.setLogo("http://logo.taobao.com/shop-logo"+shop.getPicPath());
		shop2.setNick(shop.getNick());
		shop2.setUrl("http://shop"+shop.getSid()+".taobao.com");
		return shop2;
	}

	
}
